<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="close">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened :collapse="isCollapse"
          :collapse-transition="false" router
          :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu v-for="i in menuList" :key="i.id" :index="i.id+''">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 字体图标 -->
              <i :class="iconObj[i.id]"></i>
              <!-- 文本 -->
              <span>{{i.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="s in i.children" :key="s.children.id" 
            :index="'/'+s.path" @click="saveNavState('/'+s.path)">
              <template slot="title">
                <!-- 字体图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{s.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
    name:'Home',
    data() {
      return {
        // 左侧菜单数据
        menuList:[],
        iconObj:{
          '125':'iconfont icon-users',
          '103':'iconfont icon-tijikongjian',
          '101':'iconfont icon-shangpin',
          '102':'iconfont icon-danju',
          '145':'iconfont icon-baobiao'
        },
        // 是否折叠的参数
        isCollapse:false,
        //被激活的地址
        activePath:''
      }
    },
    methods:{
      close(){
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      // 获取左侧菜单
      async getMenuList(){
        const {data:res} = await this.$http.get('menus')
        if(res.meta.status!==200) return this.$message.error(res.meta.msg);
        this.menuList=res.data 
        // console.log(res);
      },
      //左侧导航栏折叠与展开功能
      toggleCollapse(){
        this.isCollapse=!this.isCollapse

      },
      saveNavState(val){
        sessionStorage.setItem('activePath',val)
        this.activePath=val
      }
    },
    created(){
      this.getMenuList()
      this.activePath=sessionStorage.getItem('activePath')
    }
}
</script>

<style>
.home-container{
  height: 100%;
  width: 100%;
}
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0;
  color: #fff;
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div img {
  margin-right: 10px;
}
.el-aside{
  background-color: #333744;
}
.el-menu{
  border-right: none;
}
.el-main{
  background-color: #eaedf1;
}
.iconfont{
  vertical-align: middle;
  margin-right: 10px;
}
.toggle-button{
  background-color: #4a5064;
  text-align: center;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;

}
</style>